Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

181
Views
Quiero alternar la clase .textDecoration cuando hago clic en "TODOS" los elementos h1. ¿Me pueden ayudar con este código?

Solo puedo acceder al primer elemento en h1. También puedo crear un botón y hacer clic en un bucle a través de los elementos h1 y activar o desactivar la clase .textDecoration. Pero mi objetivo es hacer clic en el elemento que quiero paso a paso y activar o desactivar la clase .textDecoration.

 var x = document.getElementsByTagName("h1")[0]; function myFunction() { x.classList.toggle("textDecoration"); } x.addEventListener("click", myFunction);
 .textDecoration { text-decoration: line-through; }
 <h1>Text-Decoration: line-through</h1> <h1>Text-Decoration: line-through</h1> <h1>Text-Decoration: line-through</h1> <h1>Text-Decoration: line-through</h1>

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

te sugiero que delegues

 document.getElementById("container").addEventListener("click", function(e) { const tgt = e.target.closest("h1"); if (tgt) tgt.classList.toggle("textDecoration"); })
 .textDecoration { text-decoration: line-through; }
 <div id="container"> <h1>Text-Decoration: line-through</h1> <h1>Text-Decoration: line-through</h1> <h1>Text-Decoration: line-through</h1> <h1>Text-Decoration: line-through</h1> </div>

about 4 years ago · Juan Pablo Isaza Report

0

Tu pregunta me parece muy mal formulada, ¿buscas eso?

 document.querySelectorAll('h1').forEach( (Hn,_,All_h1) => { Hn.onclick =_=> { if (Hn.classList.toggle('textDecoration')) All_h1.forEach( Hx=> Hx.classList.toggle('textDecoration', Hn===Hx) ) } })
 .textDecoration { text-decoration: line-through; }
 <h1>Text-Decoration: line-through</h1> <h1>Text-Decoration: line-through</h1> <h1>Text-Decoration: line-through</h1> <h1>Text-Decoration: line-through</h1>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!